package com.frog.service.friend.service.friendList;


import com.frog.common.util.MapBuilderUtil;
import com.frog.service.friend.common.CommonStringUtil;
import com.frog.service.friend.enums.FriendListTypeEnum;
import com.frog.service.friend.model.RelationModel;
import com.frog.service.friend.model.domain.FriendFollowingRelationshipDO;
import com.frog.service.friend.service.factory.FriendListFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MaintainRelationServiceImpl implements MaintainRelationService {

    @Autowired
    private FriendListFactory factory;

    /**
     * 添加好友关系
     */
    @Override
    public void addFollow(RelationModel relationModel) {
        // 用户添加好友,根据是否是发起人的条件添加到friendList
        // 1,userId是发起人
        //   如果 status == 0 , status_later == 3 下, 分别添加, followed(将friendUserId添加到userId里面), followedMe(将userId添加到friendUserId里面)
        //   如果 status == 1 , status_later == 2 下, 分别添加, followed(将friendUserId添加到userId里面), followedMe(将userId添加到friendUserId里面)
        //                                                                  followedMutual(将friendUserId添加到userId里面),followedMutual(将userId添加到friendUserId里面)
        //   否则不做任何处理

        // 2,friendUserId是发起人
        //   如果 status == 0 , status_later == 1 下, 分别添加, followed(将userId添加到friendUserId里面), followedMe(将friendUserId添加到userId里面)
        //   如果 status == 3 , status_later == 2 下, 分别添加  followed(将userId添加到friendUserId里面), followedMe(将friendUserId添加到userId里面)
        //                                                                  followedMutual(将friendUserId添加到userId里面),followedMutual(将userId添加到friendUserId里面)
        //   否则不做任何处理

        if (!relationModel.isSuccessStatus()) return;

        AbstractFriendListStrategy followed = factory.getService(FriendListTypeEnum.followed);
        AbstractFriendListStrategy followedMe = factory.getService(FriendListTypeEnum.followedMe);
        AbstractFriendListStrategy follwedMutual = factory.getService(FriendListTypeEnum.follwedMutual);

        double sorce = System.currentTimeMillis() / 1000.0;
        String fromUserId = String.valueOf(relationModel.getFromUserId());
        String toUserId = String.valueOf(relationModel.getToUserId());

        followed.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
        followedMe.zadd(toUserId, MapBuilderUtil.put(fromUserId, sorce).build());

        if (relationModel.isMutualFriendAfter()) {
            follwedMutual.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
            follwedMutual.zadd(toUserId, MapBuilderUtil.put(fromUserId, sorce).build());
        }
    }


    /**
     * 删除好友关系
     */
    @Override
    public void delFollow(RelationModel relationModel) {
        // 用户取消好友,根据是发起人的条件删除friendList
        // 1, userId是发起人
        //    如果 status = 3 , status_later = 0 , 分别删除, followed(将friendUserId从userId中删除), followedMe(将userId从friendUserId中删除)
        //    如果 status = 2 , status_later = 1 , 分别删除, followed(将friendUserId从userId中删除), followedMe(将userId从friendUserId中删除)
        //                                                               followedMutual(将friendUserId从userId中删除),followedMutual(将userId从friendUserId中删除)
        // 否则不做任何处理

        // 2, friendUserId是发起人
        //    如果 status == 1 , status_later == 0 , 分别删除, followed(将userId从friendUserId中删除), followedMe(将friendUserId从userId中删除)
        //    如果 status == 2 , status_later == 3 , 分别删除, followed(将userId从friendUserId中删除), followedMe(将friendUserId从userId中删除)
        //                                                               followedMutual(将userId从friendUserId中删除),followedMutual(将friendUserId从userId中删除)
        // 否则不做任何处理

        if (!relationModel.isSuccessStatus()) return;

        AbstractFriendListStrategy followed = factory.getService(FriendListTypeEnum.followed);
        AbstractFriendListStrategy followedMe = factory.getService(FriendListTypeEnum.followedMe);
        AbstractFriendListStrategy follwedMutual = factory.getService(FriendListTypeEnum.follwedMutual);

        String fromUserId = String.valueOf(relationModel.getFromUserId());
        String toUserId = String.valueOf(relationModel.getToUserId());

        followed.zrem(fromUserId, toUserId);
        followedMe.zrem(toUserId, fromUserId);

        if (relationModel.isMutualFriendBeter()) {
            follwedMutual.zrem(fromUserId, toUserId);
            follwedMutual.zrem(toUserId, fromUserId);
        }
    }


    /**
     * 添加拉黑
     */
    @Override
    public void addBlock(RelationModel relationModel) {

        if (!relationModel.isSuccessStatus()) return;

        String fromUserId = String.valueOf(relationModel.getFromUserId());
        String toUserId = String.valueOf(relationModel.getToUserId());
        double sorce = System.currentTimeMillis() / 1000.0;

        // 添加拉黑friendList
        AbstractFriendListStrategy blocked = factory.getService(FriendListTypeEnum.blocked);
        AbstractFriendListStrategy blockedMe = factory.getService(FriendListTypeEnum.blockedMe);
        AbstractFriendListStrategy blockedMutual = factory.getService(FriendListTypeEnum.blockedMutual);

        blocked.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
        blockedMe.zadd(toUserId, MapBuilderUtil.put(fromUserId, sorce).build());

        if (relationModel.isMutualFriendAfter()) {
            blockedMutual.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
            blockedMutual.zadd(toUserId, MapBuilderUtil.put(fromUserId, sorce).build());
        }

        // 删除好友friendList
        AbstractFriendListStrategy followed = factory.getService(FriendListTypeEnum.followed);
        AbstractFriendListStrategy follwedMutual = factory.getService(FriendListTypeEnum.follwedMutual);

        followed.zrem(fromUserId, toUserId);
        follwedMutual.zrem(fromUserId, toUserId);
    }


    /**
     * 取消拉黑
     */
    @Override
    public void delBlock(FriendFollowingRelationshipDO f, RelationModel relationModel) {
        if (!relationModel.isSuccessStatus()) return;


        String fromUserId = String.valueOf(relationModel.getFromUserId());
        String toUserId = String.valueOf(relationModel.getToUserId());

        // 取消拉黑
        AbstractFriendListStrategy blocked = factory.getService(FriendListTypeEnum.blocked);
        AbstractFriendListStrategy blockedMe = factory.getService(FriendListTypeEnum.blockedMe);
        AbstractFriendListStrategy blockedMutual = factory.getService(FriendListTypeEnum.blockedMutual);

        blocked.zrem(fromUserId, toUserId);
        blockedMe.zrem(toUserId, fromUserId);

        if (relationModel.isMutualFriendBeter()) {
            blockedMutual.zrem(fromUserId, toUserId);
            blockedMutual.zrem(toUserId, fromUserId);
        }

        // 添加好友friendlist
        AbstractFriendListStrategy followed = factory.getService(FriendListTypeEnum.followed);
        AbstractFriendListStrategy follwedMutual = factory.getService(FriendListTypeEnum.follwedMutual);

        double sorce = System.currentTimeMillis() / 1000.0;
        followed.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());

        if (CommonStringUtil.FriendBothStatus.mutualConcern.equals(f.getFollowStatus())) {
            follwedMutual.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
        }
    }


    /**
     * 添加bestfriend关系
     */
    @Override
    public void addBestfriend(RelationModel relationModel) {
        // 用户添加bestfriend,根据是否是发起人的条件添加到friendList
        // 1,userId是发起人
        //   如果 status == 0 , status_later == 3 下, 分别添加, bestFriended(将friendUserId添加到userId里面), bestFriendedMe(将userId添加到friendUserId里面)
        //   如果 status == 1 , status_later == 2 下, 分别添加, bestFriended(将friendUserId添加到userId里面), bestFriendedMe(将userId添加到friendUserId里面)
        //                                                                  followedMutual(将friendUserId添加到userId里面),followedMutual(将userId添加到friendUserId里面)
        //   否则不做任何处理

        // 2,friendUserId是发起人
        //   如果 status == 0 , status_later == 1 下, 分别添加, bestFriended(将userId添加到friendUserId里面), bestFriendedMe(将friendUserId添加到userId里面)
        //   如果 status == 3 , status_later == 2 下, 分别添加  bestFriended(将userId添加到friendUserId里面), bestFriendedMe(将friendUserId添加到userId里面)
        //                                                                  bestFriendedMutual(将friendUserId添加到userId里面),followedMutual(将userId添加到friendUserId里面)
        //   否则不做任何处理

        if (!relationModel.isSuccessStatus()) return;

        AbstractFriendListStrategy bestFriended = factory.getService(FriendListTypeEnum.bestFriended);
        AbstractFriendListStrategy bestFriendedMe = factory.getService(FriendListTypeEnum.bestFriendedMe);
        AbstractFriendListStrategy bestFriendedMutual = factory.getService(FriendListTypeEnum.bestFriendedMutual);

        double sorce = System.currentTimeMillis() / 1000.0;
        String fromUserId = String.valueOf(relationModel.getFromUserId());
        String toUserId = String.valueOf(relationModel.getToUserId());

        bestFriended.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
        bestFriendedMe.zadd(toUserId, MapBuilderUtil.put(fromUserId, sorce).build());

        if (relationModel.isMutualFriendAfter()) {
            bestFriendedMutual.zadd(fromUserId, MapBuilderUtil.put(toUserId, sorce).build());
            bestFriendedMutual.zadd(toUserId, MapBuilderUtil.put(fromUserId, sorce).build());
        }
    }

    /**
     * 取消bestfriend关系
     */
    @Override
    public void delBestfriend(RelationModel relationModel) {
        // 用户取消best,根据是发起人的条件删除friendList
        // 1, userId是发起人
        //    如果 status = 3 , status_later = 0 , 分别删除, bestFriended(将friendUserId从userId中删除), bestFriendedMe(将userId从friendUserId中删除)
        //    如果 status = 2 , status_later = 1 , 分别删除, bestFriended(将friendUserId从userId中删除), bestFriendedMe(将userId从friendUserId中删除)
        //                                                               bestFriendedMutual(将friendUserId从userId中删除),bestFriendedMutual(将userId从friendUserId中删除)
        // 否则不做任何处理

        // 2, friendUserId是发起人
        //    如果 status == 1 , status_later == 0 , 分别删除, bestFriended(将userId从friendUserId中删除), bestFriendedMe(将friendUserId从userId中删除)
        //    如果 status == 2 , status_later == 3 , 分别删除, bestFriended(将userId从friendUserId中删除), bestFriendedMe(将friendUserId从userId中删除)
        //                                                               bestFriendedMutual(将userId从friendUserId中删除),bestFriendedMutual(将friendUserId从userId中删除)
        // 否则不做任何处理
        if (!relationModel.isSuccessStatus()) return;

        AbstractFriendListStrategy bestFriended = factory.getService(FriendListTypeEnum.bestFriended);
        AbstractFriendListStrategy bestFriendedMe = factory.getService(FriendListTypeEnum.bestFriendedMe);
        AbstractFriendListStrategy bestFriendedMutual = factory.getService(FriendListTypeEnum.bestFriendedMutual);

        String fromUserId = String.valueOf(relationModel.getFromUserId());
        String toUserId = String.valueOf(relationModel.getToUserId());

        bestFriended.zrem(fromUserId, toUserId);
        bestFriendedMe.zrem(toUserId, fromUserId);

        if (relationModel.isMutualFriendBeter()) {
            bestFriendedMutual.zrem(fromUserId, toUserId);
            bestFriendedMutual.zrem(toUserId, fromUserId);
        }
    }
}